package com.hd.trans.framework.dialog;

import android.app.Dialog;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.hd.trans.R;
import com.hd.trans.db.DataBaseMgr;
import com.hd.trans.db.bean.HuDunLanguage;
import com.hd.trans.framework.click.XClickUtil;
import com.hd.trans.network.PreferenceMgr;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class SelectTransLanguageDialog extends DialogFragment implements View.OnClickListener {

    private static final String ARG_IS_VOICE = "arg_is_voice";
    private static final String ARG_IS_FROM = "arg_is_from";
    private static final String ARG_IS_NATION = "arg_is_Nation";
    private static final String ARG_FROM_LAN = "arg_from_lan";
    private static final String ARG_TO_LAN = "arg_to_lan";

    private boolean isVoice;
    private boolean isNation;
    private boolean isFromPage;

    private OnLanguageChangedListener mOnLanguageChangedListener;
    private TextView title;
    private RecyclerView rvBefore;
    private RecyclerView rvAfter;
    private TextView textFrom;
    private TextView textTo;
    private TextView btnCancel;
    private TextView btnConfirm;

    private List<HuDunLanguage> langListBefore;
    private List<HuDunLanguage> langListAfter;
    private TransLanguageAdapter adapterBefore;
    private TransLanguageAdapter adapterAfter;
    private HuDunLanguage fromLanguage;
    private HuDunLanguage toLanguage;

    public interface OnLanguageChangedListener {
        void onLanguageChanged(HuDunLanguage fromLanguage, HuDunLanguage toLanguage);
    }

    public SelectTransLanguageDialog setOnLanguageChangedListener(OnLanguageChangedListener onLanguageChangedListener) {
        this.mOnLanguageChangedListener = onLanguageChangedListener;
        return this;
    }

    public static SelectTransLanguageDialog newInstance(HuDunLanguage fromLanguage, HuDunLanguage toLanguage,
                                                   boolean isFromPage, boolean isVoice, boolean isNation) {
        SelectTransLanguageDialog instance = new SelectTransLanguageDialog();
        Bundle args = new Bundle();
        args.putParcelable(ARG_FROM_LAN, fromLanguage);
        args.putParcelable(ARG_TO_LAN, toLanguage);
        args.putBoolean(ARG_IS_VOICE, isVoice);
        args.putBoolean(ARG_IS_FROM, isFromPage);
        args.putBoolean(ARG_IS_NATION, isNation);

        instance.setArguments(args);
        return instance;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置dialog样式
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.BottomDialogStyle);
    }

    @Override
    public void onStart() {
        super.onStart();
        //可以在createView里一次写
        Dialog dialog = getDialog();
        //设置dialog 宽高
        if (dialog != null) {
            int width = Resources.getSystem().getDisplayMetrics().widthPixels;
            dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            dialog.getWindow().setGravity(Gravity.BOTTOM);
        }
    }


    private void initDialog() {
        Dialog dialog = getDialog();
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));//设置背景为透明
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_trans_select_lang, container, false);
        initView(view);
        initDialog();
        initData();
        return view;
    }

    private void initData() {
        Bundle args = getArguments();
        if (args != null) {
            fromLanguage = args.getParcelable(ARG_FROM_LAN);
            toLanguage = args.getParcelable(ARG_TO_LAN);
            isVoice = args.getBoolean(ARG_IS_VOICE);
            isFromPage = args.getBoolean(ARG_IS_FROM);
            isNation = args.getBoolean(ARG_IS_NATION);
        }
        textFrom.setText(fromLanguage.getName());
        textTo.setText(toLanguage.getName());
        setTranTypeStatus(isFromPage);
        initLeftLanguages();
        initRightLanguages();
    }

    private void initView(View view) {
        title = view.findViewById(R.id.title);
        rvBefore = view.findViewById(R.id.rv_before);
        rvAfter = view.findViewById(R.id.rv_after);
        textFrom = view.findViewById(R.id.txt_from);
        textTo = view.findViewById(R.id.txt_to);
        btnCancel = view.findViewById(R.id.btn_cancel);
        btnConfirm = view.findViewById(R.id.btn_confirm);

        textFrom.setOnClickListener(this);
        textTo.setOnClickListener(this);
        btnCancel.setOnClickListener(this);
        btnConfirm.setOnClickListener(this);
    }


    private void initLeftLanguages() {
        langListBefore = DataBaseMgr.getInstance().getDistinguishLanguages();
        adapterBefore = new TransLanguageAdapter(langListBefore);
        adapterBefore.setCheckedItem(fromLanguage);
        rvBefore.setLayoutManager(new LinearLayoutManager(getContext()));
        rvBefore.setItemAnimator(new DefaultItemAnimator());
        rvBefore.setAdapter(adapterBefore);
        adapterBefore.setOnItemClickListener(beforeItemClickListener);
    }
    private void initRightLanguages() {
        langListAfter = DataBaseMgr.getInstance().getAllLanguages(false);
        adapterAfter = new TransLanguageAdapter(langListAfter);
        adapterAfter.setCheckedItem(toLanguage);
        rvAfter.setLayoutManager(new LinearLayoutManager(getContext()));
        rvAfter.setItemAnimator(new DefaultItemAnimator());
        rvAfter.setAdapter(adapterAfter);
        adapterAfter.setOnItemClickListener(afterItemClickListener);
    }

    private OnItemClickListener beforeItemClickListener = new OnItemClickListener() {
        @Override
        public void onItemClick(@NonNull @NotNull BaseQuickAdapter adapter, @NonNull @NotNull View view, int position) {
            Object item = adapterBefore.getItem(position);
            if (item instanceof HuDunLanguage) {
                HuDunLanguage checkedItem = (HuDunLanguage) item;
                if (checkedItem != null) {
                    if (!checkedItem.equals(adapterBefore.getCheckedItem())) {
                        fromLanguage = checkedItem;
                        textFrom.setText(checkedItem.getName());
                        adapterBefore.setCheckedItem(checkedItem);
                    }
                }
            }
        }
    };

    private OnItemClickListener afterItemClickListener = new OnItemClickListener() {
        @Override
        public void onItemClick(@NonNull @NotNull BaseQuickAdapter adapter, @NonNull @NotNull View view, int position) {
            Object item = adapterAfter.getItem(position);
            if (item instanceof HuDunLanguage) {
                HuDunLanguage checkedItem = (HuDunLanguage) item;
                if (checkedItem != null) {
                    if (!checkedItem.equals(adapterAfter.getCheckedItem())) {
                        toLanguage = checkedItem;
                        textTo.setText(checkedItem.getName());
                        adapterAfter.setCheckedItem(checkedItem);
                    }
                }
            }
        }
    };

    private void setTranTypeStatus(boolean isFromPage){
        title.setText(isFromPage?"请选择识别语种":"请选择翻译语种");

        textFrom.setSelected(isFromPage);
        textTo.setSelected(!isFromPage);

        rvBefore.setVisibility(isFromPage?View.VISIBLE:View.GONE);
        rvAfter.setVisibility(isFromPage?View.GONE:View.VISIBLE);
    }

    @Override
    public void onClick(View v) {
        if(XClickUtil.isFastDoubleClick(v,XClickUtil.INTERVAL_MILLIS)){
            return;
        }
        int viewId = v.getId();
        if(viewId == R.id.txt_from){
            isFromPage = true;
            setTranTypeStatus(true);
        }else if(viewId ==R.id.txt_to){
            isFromPage = false;
            setTranTypeStatus(false);
        }else if(viewId ==R.id.btn_cancel){
            dismiss();
        }else if(viewId ==R.id.btn_confirm){
            if (mOnLanguageChangedListener != null){
                mOnLanguageChangedListener.onLanguageChanged(fromLanguage, toLanguage);
                PreferenceMgr.getInstance().getTranslatePreference().saveWorTransFromLanguage(fromLanguage.getName());
                PreferenceMgr.getInstance().getTranslatePreference().saveWorTransToLanguage(toLanguage.getName());
            }
            dismiss();
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
